<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>部署管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="名称" class="filter-item handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" class="filter-item" @click="handleAdd">新增</el-button>
                <el-button type="primary" icon="el-icon-message-solid" class="filter-item" @click="handleStatus">状态</el-button>
                <el-button type="primary" icon="el-icon-circle-check" class="filter-item" @click="handleStart">启动</el-button>
                <el-button type="primary" icon="el-icon-circle-close" class="filter-item" @click="handleStop">停止</el-button>
            </div>
            <el-table
                    v-loading="loading"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column label="应用" align="center">
                    <template slot-scope="scope">
                        {{selectDataField(appList, scope.row.appId)}}
                    </template>
                </el-table-column>
                <el-table-column prop="serverIds" label="服务器" align="center">
                    <template slot-scope="scope">
                        {{selectDataFields(serverList, scope.row.serverIds, ',')}}
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseDateTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="220" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="应用" >
                    <el-select v-model="form.appId" placeholder="请选择">
                        <el-option
                                v-for="item in appList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="服务器" >
                    <el-select v-model="form.serverIdArr" multiple placeholder="请选择">
                        <el-option
                                v-for="item in serverList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { deployPage,deploySave,deployUpdate,deployStatus,deployStart,deployStop,deployDelete } from '../../../api/deploy/deploy';
import { deployAppList } from '../../../api/deploy/deployApp';

export default {
    name: 'deploy-list',
    data() {
        return {
            // 遮罩层
            loading: true,
            query: {
                name:'',
                current: 1,
                size: 10
            },
            editVisible:false,
            form:{
                appId:"",
                serverIds:"",
                serverIdArr:[]
            },
            appList:[],
            serverList:[],
            tableData: [],
            pageTotal: 0
        };
    },
    created() {
        deployAppList({}).then(res => {
            this.appList = res.appList;
            this.serverList = res.serverList;
            this.defaultform = JSON.parse(JSON.stringify(this.form));
        });
        this.getData();
    },
    destroyed() {
        this.defaultform = null;
    },
    methods: {
        getData() {
            this.loading = true;
            deployPage(this.query).then(res => {
                this.loading = false;
                // 获取值
                this.tableData = res.data;
                this.pageTotal = res.count;
            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    deployDelete(row.id).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`删除成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                })
                .catch(() => {});
        },
        handleStatus() {
            let selection = this.$refs.multipleTable.selection;
            if (selection == null || selection.length <= 0) {
                this.$message.error(`请选择`);
                return false;
            }
            if (selection.length > 1) {
                this.$message.error(`只能选择一项`);
                return false;
            }
            let id = selection[0].id;
            deployStatus(id).then(res => {
                if (res.code == 0) {
                    this.editVisible = false;
                    this.$message.success(res.msg);
                    this.getData();
                } else {
                    this.$message.error(res.msg);
                    return false;
                }
            });
        },
        handleStart() {
            let selection = this.$refs.multipleTable.selection;
            if (selection == null || selection.length <= 0) {
                this.$message.error(`请选择`);
                return false;
            }
            if (selection.length > 1) {
                this.$message.error(`只能选择一项`);
                return false;
            }
            let id = selection[0].id;
            deployStart(id).then(res => {
                if (res.code == 0) {
                    this.editVisible = false;
                    this.$message.success(res.msg);
                    this.getData();
                } else {
                    this.$message.error(res.msg);
                    return false;
                }
            });
        },
        handleStop() {
            let selection = this.$refs.multipleTable.selection;
            if (selection == null || selection.length <= 0) {
                this.$message.error(`请选择`);
                return false;
            }
            if (selection.length > 1) {
                this.$message.error(`只能选择一项`);
                return false;
            }
            let id = selection[0].id;
            deployStop(id).then(res => {
                if (res.code == 0) {
                    this.editVisible = false;
                    this.$message.success(res.msg);
                    this.getData();
                } else {
                    this.$message.error(res.msg);
                    return false;
                }
            });
        },
        handleAdd(){
            this.form = Object.assign({}, this.defaultform);
            this.editVisible = true;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        handleEdit(index, row) {
            this.form.id = row.id;
            this.form.appId = row.appId;
            this.form.serverIds = row.serverIds;
            let serverIds = this.form.serverIds;
            // arr
            let serverIdArr = [];
            if (serverIds != null && serverIds != "") {
                serverIds.split(',').forEach(item => {
                    serverIdArr.push(parseInt(item));
                })
            }
            this.form.serverIdArr = serverIdArr;
            this.editVisible = true;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        // 保存编辑
        saveEdit() {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    console.log('error submit!!');
                    return false;
                }
                let serverIdArr = this.form.serverIdArr;
                if (serverIdArr == null || serverIdArr.length <= 0) {
                    this.$message.error(`请选择服务器`);
                    return false;
                }
                this.form.serverIds = serverIdArr.join(",");
                if (this.form.id) {
                    deployUpdate(this.form).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`修改成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                } else {
                    deploySave(this.form).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`修改成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'current', 1);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'current', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
